﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;

namespace Srvtools
{
    public partial class InfoRefPanel : Form
    {
        public InfoRefPanel()
        {
            InitializeComponent();
        }

        public bool autoPanel;
        public bool AutoPanel
        {
            get { return autoPanel; }
        }

        public bool multiSelect;
        public bool MultiSelect
        {
            get { return  multiSelect; }
        }

        private Panel panel;

        public Panel Panel
        {
            get { return panel; }
        }

        private InfoTranslate infoTranslate;

        public InfoTranslate InfoTranslate
        {
            get { return infoTranslate; }
        }

        private RefColumnsCollection columns;

        public RefColumnsCollection Columns
        {
            get { return columns; }
        }
	
        public InfoRefPanel(Panel p, InfoTranslate it) : this(p, it, false, false, null) { }

        public InfoRefPanel(Panel p, InfoTranslate it, bool ap) : this(p, it, ap, false, null) { }

        public InfoRefPanel(Panel p, InfoTranslate it, bool ap, bool ms) : this(p, it, ap, ms, null) { }

        public InfoRefPanel(Panel p, InfoTranslate it, bool ap, bool ms, RefColumnsCollection col):this()
        {
            panel = p;
            infoTranslate = it;
            autoPanel = ap;
            multiSelect = ms;
            columns = col;
        }

        //end add
        private void InfoRefPanel_Load(object sender, EventArgs e)
        {
            String[] texts = SysMsg.GetSystemMessage(CliUtils.fClientLang, "Srvtools", "InfoRefPanel", "Text").Split(';');
            this.btnOK.Text = texts[0];
            this.btnCancel.Text = texts[1];
            this.btnQuery.Text = texts[2];

            if (this.AutoPanel)
            {
                if (this.Columns != null && this.Columns.Count > 0)
                {
                    this.infoDataGridView1.AutoGenerateColumns = false;
                    foreach (RefColumns column in this.Columns)
                    {
                        DataGridViewTextBoxColumn textBoxColumn = new DataGridViewTextBoxColumn();
                        textBoxColumn.Name = column.Column;
                        textBoxColumn.DataPropertyName = column.Column;
                        textBoxColumn.HeaderText = column.HeaderText;
                        textBoxColumn.DefaultCellStyle = column.DefaultCellStyle;
                        textBoxColumn.Width = column.Width;
                        textBoxColumn.Visible = column.Visible;
                        this.infoDataGridView1.Columns.Add(textBoxColumn);
                    }
                }
                this.infoDataGridView1.DataSource = this.InfoTranslate.BindingSource;
            foreach (DataGridViewColumn dgvc in this.infoDataGridView1.Columns)
            {
                if (dgvc.Name == "SelectItems")
                    dgvc.HeaderText = texts[3];
                else
                {
                    if (this.Columns == null || this.Columns.Count == 0)
                    {
                        dgvc.HeaderText = GetHeaderText(dgvc.DataPropertyName);
                    }
                    dgvc.ReadOnly = true;
                }
            }
            }
            else
            {
                this.btnQuery.Visible = false;

                this.infoDataGridView1.Visible = false;
                this.panel1.Controls.Add(this.Panel);
                this.Panel.Dock = DockStyle.Fill;
                this.Panel.Show();
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        public ArrayList value;
        public ArrayList display;
        private void btnOK_Click(object sender, EventArgs e)
        {
            value = new ArrayList();
            display = new ArrayList();
            if (this.InfoTranslate.RefReturnFields.Count > 0)
            {
                if (this.AutoPanel)
                {
                    for (int i = 0; i < this.InfoTranslate.RefReturnFields.Count; i++)
                    {
                        String ColumnName = ((TranslateRefReturnFields)(this.InfoTranslate.RefReturnFields[i])).ColumnName;
                        String DisplayColumnName = ((TranslateRefReturnFields)(this.InfoTranslate.RefReturnFields[i])).DisplayColumnName;

                        String values = String.Empty;
                        String displays = String.Empty;
                        for (int j = 0; j < this.infoDataGridView1.Rows.Count; j++)
                        {
                            if (this.infoDataGridView1.Rows[j].Cells["SelectItems"].Value != null && (bool)this.infoDataGridView1.Rows[j].Cells["SelectItems"].Value)
                            {
                                values += this.infoDataGridView1.Rows[j].Cells[ColumnName].Value + ",";
                                displays += this.infoDataGridView1.Rows[j].Cells[DisplayColumnName].Value + ",";
                            }
                        }
                        if (values.Length > 0)
                            values = values.Remove(values.Length - 1);
                        if (displays.Length > 0)
                            displays = displays.Remove(displays.Length - 1);

                        value.Add(values);
                        display.Add(displays);
                    }
                }
                else
                {
                    for (int i = 0; i < this.InfoTranslate.RefReturnFields.Count; i++)
                    {
                        String ColumnName = ((TranslateRefReturnFields)(this.InfoTranslate.RefReturnFields[i])).ColumnName;
                        String DisplayColumnName = ((TranslateRefReturnFields)(this.InfoTranslate.RefReturnFields[i])).DisplayColumnName;

                        //add multiSelect by luciferling 20080730
                        if (this.MultiSelect)
                        {
                            String values = String.Empty;
                            String displays = String.Empty;
                            InfoDataGridView idgv = (InfoDataGridView)this.Panel.Controls[0];

                            int m, n;
                            for (m = 0; m < idgv.ColumnCount; m++)
                            {
                                if (idgv.Columns[m].DataPropertyName.ToString() == ColumnName)
                                    break;
                            }
                            for (n = 0; n < idgv.ColumnCount; n++)
                            {
                                if (idgv.Columns[n].DataPropertyName.ToString() == DisplayColumnName)
                                    break;
                            }


                            for (int k = 0; k < idgv.SelectedRows.Count; k++)
                            {
                                values += idgv.SelectedRows[k].Cells[m].Value + ",";
                                displays += idgv.SelectedRows[k].Cells[n].Value + ",";
                            }
                            if (values.Length > 0)
                                values = values.Remove(values.Length - 1);
                            if (displays.Length > 0)
                                displays = displays.Remove(displays.Length - 1);

                            value.Add(values);
                            display.Add(displays);
                        }
                            //end add
                        else
                        {
                            value.Add(((DataRowView)this.InfoTranslate.BindingSource.Current).Row[ColumnName].ToString());
                            display.Add(((DataRowView)this.InfoTranslate.BindingSource.Current).Row[DisplayColumnName].ToString());
                        }
                    }
                }
            }
            this.Hide();
            AfterOKEventArgs args = new AfterOKEventArgs();
            args.Values = value;
            args.Displays = display;
            OnAfterOK(args);
        }

        static readonly object afterOKEventKey = new object();

        public event EventHandler<AfterOKEventArgs> AfterOK
        {
            add { Events.AddHandler(afterOKEventKey, value); }
            remove { Events.RemoveHandler(afterOKEventKey, value); }
        }
        protected void OnAfterOK(AfterOKEventArgs args)
        {
            EventHandler<AfterOKEventArgs> handler = (EventHandler<AfterOKEventArgs>)Events[afterOKEventKey];
            if (handler != null)
            {
                handler(this, args);
            }
        }

        private String GetHeaderText(String ColName)
        {
            string strTableName = this.InfoTranslate.BindingSource.GetTableName();
            DataSet ds = DBUtils.GetDataDictionary(this.InfoTranslate.BindingSource, this.DesignMode);

            String strHeaderText = ColName;
            if (ds != null && ds.Tables.Count > 0)
            {
                int i = ds.Tables[strTableName].Rows.Count;
                for (int j = 0; j < i; j++)
                {
                    if (ds.Tables[strTableName].Rows[j]["FIELD_NAME"].ToString().ToLower() == ColName.ToLower())
                    {
                        if (strHeaderText != String.Empty)
                            strHeaderText = ds.Tables[strTableName].Rows[j]["CAPTION"].ToString();
                    }
                }
            }

            return strHeaderText;
        }

        private string strFilter = string.Empty;
        private void btnQuery_Click(object sender, EventArgs e)
        {
            InfoRefVal aInfoRefVal = new InfoRefVal();
            aInfoRefVal.DataSource = this.InfoTranslate.BindingSource;
            frmRefButtonQuery afrmRefButtonQuery = null;
            int i = infoDataGridView1.Columns.Count;
            string[] colName = new String[i];
            String[] colCaption = new String[i];
            for (int j = 0; j < i; j++)
            {
                colName[j] = infoDataGridView1.Columns[j].Name;
                colCaption[j] = infoDataGridView1.Columns[j].HeaderText;
            }
            afrmRefButtonQuery = new frmRefButtonQuery(colName, colCaption, infoDataGridView1.DataSource as InfoBindingSource, strFilter, aInfoRefVal);
            afrmRefButtonQuery.Font = this.Font;
            afrmRefButtonQuery.ShowDialog();
        }
    }

    public class AfterOKEventArgs : EventArgs
    {
        ArrayList _values = null;
        ArrayList _displays = null;

        public ArrayList Values
        {
            get { return _values; }
            set { _values = value; }
        }

        public ArrayList Displays
        {
            get { return _displays; }
            set { _displays = value; }
        }
    }
}